<?php
class Benew_model extends CI_Model
{
	public function __construct()
	{
		$this->load->library('session');
		$this->load->database();
	}
	public function getTopics()
	{
		$sql = 'SELECT auto_id,heading,intro from '.CI_Model::$topic.' WHERE user_id = ?;';
		return $this->db->query($sql,array($_SESSION['id']))->result_array();
	}

	public function getTopicById($id)
	{
		$sql = 'SELECT auto_id,heading,intro,type,pic_id FROM '.CI_Model::$topic.' WHERE auto_id= ? AND user_id = ?;';
		return $this->db->query($sql,array($id,$_SESSION['id']))->row_array();
	}
	
	public function getIfTopic($id)
	{
		$sql = 'SELECT auto_id FROM '.CI_Model::$topic.' WHERE auto_id = ? AND user_id = ?;';
		$result = $this->db->query($sql,array($id,$_SESSION['id']))->row_array();
		return isset($result['auto_id']);
	}

	public function updateTopic($data)
	{
		$sql = 'UPDATE '.CI_Model::$topic.' SET heading = ? ,intro = ? ,type =?,pic_id=? WHERE auto_id = ?;';
		return $this->db->query($sql,array($data['name'],$data['detail'],$data['type'],$data['pic_id'],$data['topic_id']));
	}
	
	public function insertTopic($data)
	{
		if(!isset($data['pic_id']))
		{
			$data['pic_id'] = 0;
		}
		$sql = 'INSERT INTO '.CI_Model::$topic.' (auto_id,heading,intro,type,pic_id,user_id) VALUES (null,?,?,?,?,?) ;';
		return $this->db->query($sql,array($data['name'],$data['detail'],$data['type'],$data['pic_id'],$_SESSION['id']));
	}
}